package com.example.tiantian;

import com.example.tiantian.application.ApplicationActivity;
import com.example.tiantian.battery.NewBatteryActivity;
import com.example.tiantian.camera.CameraActivity;
import com.example.tiantian.clock.ClockActivity;
import com.example.tiantian.contact.NewContactActivity;
import com.example.tiantian.process.NewProcessActivity;

import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.SparseArray;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnLongClickListener;
import android.widget.ImageView;
import android.widget.TextView;

public class MenuActivity extends BaseActivity implements OnClickListener, OnLongClickListener {

    TextView txtMenu;
    ImageView imgContact;
    ImageView imgApp;
    ImageView imgProcess;
    ImageView imgBattery;
    ImageView imgClock;
    ImageView imgCamera;

    SparseArray<ImageInfo> infos;

    int lastSelectedID;

    @Override
    protected void init() {
        setContentView(R.layout.activity_menu);
        txtMenu = (TextView) findViewById(R.id.menu_text);
        imgContact = (ImageView) findViewById(R.id.menu_contact);
        imgApp = (ImageView) findViewById(R.id.menu_app);
        imgProcess = (ImageView) findViewById(R.id.menu_process);
        imgBattery = (ImageView) findViewById(R.id.menu_battery);
        imgClock = (ImageView) findViewById(R.id.menu_clock);
        imgCamera = (ImageView) findViewById(R.id.menu_camera);

        infos = new SparseArray<ImageInfo>();
    }

    @Override
    protected void event() {
        findViewById(R.id.menu_main).setOnClickListener(this);
        imgContact.setOnClickListener(this);
        imgApp.setOnClickListener(this);
        imgProcess.setOnClickListener(this);
        imgBattery.setOnClickListener(this);
        imgClock.setOnClickListener(this);
        imgCamera.setOnClickListener(this);

        imgContact.setOnLongClickListener(this);
        imgApp.setOnLongClickListener(this);
        imgProcess.setOnLongClickListener(this);
        imgBattery.setOnLongClickListener(this);
        imgClock.setOnLongClickListener(this);
        imgCamera.setOnLongClickListener(this);
    }

    @Override
    protected void loadData() {
        int[] id = explainIntegerArray(R.array.menu_view_id);
        String[] menuTexts = getResources().getStringArray(R.array.menu_texts);
        int[] images = explainIntegerArray(R.array.menu_images);
        int[] imagesSelected = explainIntegerArray(R.array.menu_images_selected);
        for (int i = 0; i < id.length; i++) {
            infos.put(id[i], new ImageInfo(menuTexts[i], BitmapFactory.decodeResource(getResources(), images[i]),
                    BitmapFactory.decodeResource(getResources(), imagesSelected[i])));
        }
        lastSelectedID = id[0];
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        ImageView lastView = (ImageView) findViewById(lastSelectedID);
        if (id == R.id.menu_main) {
            lastView.setImageBitmap(infos.get(lastSelectedID).normal);
            txtMenu.setText("");
            return;
        }
        lastView.setImageBitmap(infos.get(lastSelectedID).normal);
        ((ImageView) v).setImageBitmap(infos.get(id).selected);
        txtMenu.setText(infos.get(id).text);
        lastSelectedID = id;
    }

    @Override
    public boolean onLongClick(View v) {
        switch (v.getId()) {
        case R.id.menu_clock:
            startActivity(ClockActivity.class, false);
            break;
        case R.id.menu_camera:
            startActivity(CameraActivity.class, false);
            break;
        case R.id.menu_battery:
            startActivity(NewBatteryActivity.class, false);
            break;
        case R.id.menu_process:
            startActivity(NewProcessActivity.class, false);
            break;
        case R.id.menu_app:
            startActivity(ApplicationActivity.class, false);
            break;
        case R.id.menu_contact:
            startActivity(NewContactActivity.class, false);
        default:
            break;
        }
        return false;
    }

    private int[] explainIntegerArray(int id) {
        TypedArray ta = getResources().obtainTypedArray(id);
        int[] array = new int[ta.length()];
        for (int i = 0; i < array.length; i++) {
            array[i] = ta.getResourceId(i, 0);
        }
        ta.recycle();
        return array;
    }

}

class ImageInfo {
    String text;
    Bitmap normal;
    Bitmap selected;

    public ImageInfo(String text, Bitmap normal, Bitmap selected) {
        this.text = text;
        this.normal = normal;
        this.selected = selected;
    }

}
